package com.boe.common.util.freemark.base;

import lombok.*;
import com.boe.common.util.ClassUtil;
import com.boe.common.util.freemark.P0Error;
import com.boe.common.util.freemark.BaseMethodModelFreemarker;
import freemarker.template.TemplateModelException;

import java.lang.reflect.Field;
import java.util.List;


public class FreemarkerIsObject extends BaseMethodModelFreemarker {
    @Override
    public Object exec(List list) throws TemplateModelException {
        var paramValue = getFreemarkerParameter(list.get(0));

        if (paramValue instanceof Field) {
            var type = ((Field) paramValue).getType();
            return isObject(type);
        } else if (paramValue instanceof Class) {

            return isObject((Class) paramValue);
        }

        throw new P0Error(paramValue);
    }

    private Boolean isObject(Class type) {
        if (type.isArray()) return false;
        if (ClassUtil.isCollectionType(type)) return false;
        if (ClassUtil.isSimpleType(type)) return false;
        return true;
    }
}

